//修改
function orderUpdate(){
    const roomid = document.querySelector("#updateroomid").value 
        const ordernumber = document.querySelector("#updateordernumber").value
    const Idcard = document.querySelector("#updateIdcard").value
    const orderstatus = document.querySelector("#updateorderstatus").value
    const checkintime = document.querySelector("#updatecheckintime").value
    const checkouttime = document.querySelector("#updatecheckouttime").value
    if(!(ordernumber || Idcard || orderstatus || checkintime || checkouttime)){
        return alert("请输入完整的数据!")
    }
    console.log( roomid,ordernumber,Idcard,orderstatus,checkintime,checkouttime)
    Ajax({
        type:"POST",
        url:"http://localhost/hotel/php/orderUpdate.php",
        data:{roomid,ordernumber,Idcard,orderstatus,checkintime,checkouttime},
        success(){
        // location.reload();
        console.log(1);
        }
    })




}


function getStudentInfo(){
    Ajax({
        url:"http://localhost/hotel/php/order.php",
        success(data){
            sessionStorage.setItem("studentInfo",data)
            let studentObj = JSON.parse(data);
            studentObj = studentObj.slice(0,10)
            const tbody = document.querySelector("tbody")
            studentObj.forEach((item,index)=>{
                let str = `
                <tr>
                    <td>${item.roomid}</td>
                    <td>${item.ordernumber}</td>
                    <td>${item.Idcard}</td>
                    <td>${item.orderstatus}</td>
                    <td>${item.checkintime}</td>
                    <td>${item.checkouttime}</td>
                    <td>
                        <button data-bs-toggle="modal" data-bs-target="#myModal2" class="btn btn-warning btn-sm" data-edit='edit'  id=${item.roomid}>修改</button>
                        <button class="btn btn-danger btn-sm" id=${item.roomid} data-del='del'>删除</button>
                    </td>
                </tr>
                `
                tbody.innerHTML +=str;
            })
            
        }
    })
}
getStudentInfo();

const tbody = document.querySelector("tbody")
tbody.onclick = function(e){
    if(e.target.dataset.del == 'del'){
        console.log(e.target.id);
        const result = confirm("确认删除吗?")
        if(result){
            Ajax({
                url:"http://localhost/hotel/php/orderDelete.php",
                data:{id:e.target.id},
                success(data){
                location.reload();
                }
            })
        }
    }
    //修改
    if(e.target.dataset.edit == 'edit') {
        Ajax({
            url:"http://localhost/hotel/php/orderQueryOne.php",
            data:{id:e.target.id},
            success(msg){
                msg = JSON.parse(msg)
                console.log(msg);
                console.log(msg.roomid);
                document.querySelector("#updateroomid").value = msg.roomid
                document.querySelector("#updateordernumber").value = msg.ordernumber 
                document.querySelector("#updateIdcard").value = msg.Idcard
                document.querySelector("#updateorderstatus").value = msg.orderstatus
                document.querySelector("#updatecheckintime").value = msg.checkintime
                document.querySelector("#updatecheckouttime").value = msg.checkouttime
            }
        })
    } 
}






function ok(){
    
    const ordernumber = document.querySelector("#userordernumber").value
    const Idcard = document.querySelector("#userIdcard").value
    const orderstatus = document.querySelector("#userorderstatus").value
    const checkintime = document.querySelector("#usercheckintime").value
    const checkouttime = document.querySelector("#usercheckouttime").value
    // console.log(sname,clazzid,sage,sgender);
    if(!(ordernumber || Idcard || orderstatus || checkintime || checkouttime)){
        return alert("输入完整的数据");
    }

    console.log(ordernumber,Idcard,orderstatus,checkintime,checkouttime);
    Ajax({
        type:"POST",
        url:"http://localhost/hotel/php/orderAdd.php",
        data:{ordernumber,Idcard,orderstatus,checkintime,checkouttime},
        success(){
            location.reload();
        }
    })

}
